package com.lam.system.controller;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.write.handler.WriteHandler;
import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.components.easyexcel.export.DownloadUtil;
import com.lam.common.components.easyexcel.imp.ImportUtil;
import com.lam.common.components.parameter.ParameterConfigUtil;
import com.lam.common.constant.SystemConstant;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.system.entity.SysParameter;
import com.lam.system.entity.imp.SysParameterImp;
import com.lam.system.listener.UploadSysParameterListener;
import com.lam.system.param.SysParameterParam;
import com.lam.system.service.SysParameterService;

/**
 * 参数配置Controller
 * 
 * @author admin
 * @date 2022-09-20
 */
@Log(title = "参数配置")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/sys/parameter")
public class SysParameterController extends BaseController {

    @Autowired
    private SysParameterService sysParameterService;

    /**
	 * 获取参数配置
	 * @param paramCode 参数code
	 * @return
	 */
	@Log(oper = "获取参数配置")
	@GetMapping("/getConfig")
	public Result getConfig(@RequestParam String paramCode) {
		
		if(StringUtils.isBlank(paramCode)) {
			return Result.ok(Collections.emptyList());
		}
		return Result.ok().data(ParameterConfigUtil.getValue(paramCode));
	}
	
    /**
     * 查询参数配置列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('sys:parameter:list')")
    @GetMapping("/list")
    public Result list(SysParameterParam param) {
    
        startPage();
        List<SysParameter> list = sysParameterService.selectList(param);
        return Result.ok(getTableData(list));
    }
    
    /**
     * 导出参数配置列表
     */
    @PreAuthorize("@ss.hasPm('sys:parameter:export')")
    @Log(businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result export(SysParameterParam param) {
    
        withOrderBy();
        List<SysParameter> list = sysParameterService.selectList(param);
        return Result.ok(DownloadUtil.exportExcel("参数配置导出", list, SysParameter.class));
    }
    
    @Log(businessType = BusinessType.EXPORT, oper = "下载导入模板")
    @PreAuthorize("@ss.hasPm('sys:parameter:import')")
    @GetMapping("/downloadTemplate")
    public Result downloadTemplate() {
        WriteHandler headRowCommentWriteHandler = ImportUtil.getHeadRowCommentWriteHandler(SysParameterImp.class, SysParameter.class, Add.class);
        WriteHandler dropDownWriteHandler = ImportUtil.getDropDownWriteHandler(SysParameterImp.class, Add.class);
        return Result.ok(DownloadUtil.exportExcel("参数配置导入模板", Collections.emptyList(), SysParameterImp.class, 
            headRowCommentWriteHandler, dropDownWriteHandler));
    }
    
    @Log(businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPm('sys:parameter:import')")
    @PostMapping("/importData")
    public Result importData(MultipartFile file, boolean updateSupport) {
        
        logger.info("参数配置数据导入, 开始接收文件数据...");
        UploadSysParameterListener readListener = new UploadSysParameterListener(sysParameterService);
        readListener.setUpdateSupport(updateSupport);
        ImportUtil.importData("参数配置", file, readListener);
        Map<String, Object> data = readListener.getOperResult();
        data.put("originalFilename", file.getOriginalFilename());
        return Result.ok(data);
    }
    
    /**
     * 获取参数配置详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('sys:parameter:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(sysParameterService.selectById(id));
    }
    
    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPm('sys:parameter:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody SysParameter sysParameter) {
    
    	ValidateUtil.validUnique(sysParameter, ValidOperType.ADD, SystemConstant.UNIQUE_PARAMETER_CODE, "编码已存在！");
        return toAjax(sysParameterService.insert(sysParameter));
    }
    
    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPm('sys:parameter:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody SysParameter sysParameter) {
    
    	ValidateUtil.validUnique(sysParameter, ValidOperType.UPDATE, SystemConstant.UNIQUE_PARAMETER_CODE, "编码已存在！");
        return toAjax(sysParameterService.update(sysParameter));
    }
    
    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPm('sys:parameter:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        return toAjax(sysParameterService.deleteById(id));
    }
    

}
